﻿Option Explicit On
Partial Public Class MeterialMaster
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "Material Master"
            ViewState("Tariff") = clsGet.Get_MeterailMaster("")
            gridTariff.DataSource = ViewState("Tariff")
            gridTariff.DataBind()
            If (Request.QueryString("P1") & "" = "Y") Then
                Session("EditMaster") = "Y"
            Else
                Session("EditMaster") = Nothing
            End If
            If (Request.QueryString("P2") & "" <> "Y") Then
                btnUpdate.Visible = False
            End If
        End If
        gridTariff.Columns(4).Visible = False
        gridTariff.Columns(5).Visible = False
    End Sub

    Public Sub Paginate(ByVal sender As Object, ByVal e As CommandEventArgs)
        Dim CurrentIndex As Integer = gridTariff.PageIndex
        Select Case e.CommandArgument.ToString().ToLower()
            Case "first"
                gridTariff.PageIndex = 0
            Case "prev"
                If (gridTariff.PageIndex <> 0) Then
                    gridTariff.PageIndex = CurrentIndex - 1
                End If
            Case "next"
                If (gridTariff.PageIndex <> gridTariff.PageCount) Then
                    gridTariff.PageIndex = CurrentIndex + 1
                End If
            Case "last"
                gridTariff.PageIndex = gridTariff.PageCount
        End Select
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub

    Protected Sub ddlPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim Page As GridViewRow = gridTariff.BottomPagerRow
        Dim ddlPage As DropDownList = CType(Page.Cells(0).FindControl("ddlPage"), DropDownList)
        gridTariff.PageIndex = ddlPage.SelectedIndex
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub

    Private Sub gridTariff_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gridTariff.RowCommand
        Dim Index As Integer = Convert.ToInt32(e.CommandArgument)
        If (e.CommandName = "Relation") Then
            If (gridTariff.PageIndex <> 0) Then
                Index = Index Mod (gridTariff.PageIndex * 10)
            End If
            Session("MatRelation") = gridTariff.Rows(Index).Cells(0).Text & "$" & gridTariff.Rows(Index).Cells(1).Text
            ModalPopupExtender1.Show()
        ElseIf e.CommandName = "cmdedit" Then

            ' ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('Edit " & Index.ToString() & " !!');", True)
        ElseIf e.CommandName = "cmddelete" Then
            ' ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('Delete " & Index.ToString() & " !!');", True)
        End If
    End Sub

    Private Sub gridTariff_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridTariff.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            e.Row.Cells(4).Text = "OK"
        End If
        If (e.Row.RowType = DataControlRowType.Pager) Then
            Dim Page As GridViewRow = gridTariff.BottomPagerRow
            Dim ddlPage As DropDownList = CType(e.Row.FindControl("ddlPage"), DropDownList)
            Dim lblPageCount As Label = CType(e.Row.FindControl("lbPageCount"), Label)
            If Not (ddlPage Is Nothing) Then
                For i As Integer = 0 To gridTariff.PageCount - 1
                    Dim intPageNumber As Integer = i + 1
                    Dim IsItem As New ListItem(intPageNumber.ToString())
                    If (i = gridTariff.PageIndex) Then
                        IsItem.Selected = True
                    End If
                    ddlPage.Items.Add(IsItem)
                Next
            End If
            If Not (lblPageCount Is Nothing) Then
                lblPageCount.Text = gridTariff.PageCount.ToString()
            End If
        End If
    End Sub


    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        ' Dim Connector As New SAP.Connector.Destination
        Dim ClassSAP As New Class_SAP
        Dim SAP_Data As Object = ClassSAP.SAP_OPEN()

        Dim ZBABI_INPUT As New SAP_IPORT.ZBAPI_MAT_INPUTTable
        Dim ZBABI_INFOR As New SAP_IPORT.ZBAPI_MAT_INFORTable
        Dim ZBABI_PRICE As New SAP_IPORT.ZBAPI_MAT_PRICETable
        Dim EX_SUBRC As Integer
        Try
            SAP_Data.Zbapi_Iport_Material_Master("", "", EX_SUBRC, ZBABI_INPUT, ZBABI_INFOR, ZBABI_PRICE)
            If (ZBABI_INFOR.Count > 0) Or (ZBABI_PRICE.Count > 0) Then

                '***********************************************************************
                '***********  INSERT ข้อมูล CUSTOMER *************************************
                '***********************************************************************
                Dim conn As New System.Data.SqlClient.SqlConnection(connString)
                Dim clsDB As New ClassDatabase
                Dim clsFc As New ClassFunction
                Dim condition As Boolean = True
                Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
                Application.Lock()
                conn.Open()
                ' Dim ID_Customer As String = clsDB.GeneratorID(conn, "Customer")
                ' Dim ID_BillTo As String = clsDB.GeneratorID(conn, "Customer_Billto")
                tran = clsDB.BeginTran(conn)
                For i As Integer = 0 To ZBABI_INFOR.Count - 1
                    If (clsFc.UpdateMeterial(ZBABI_INFOR.Item(i).Material, ZBABI_INFOR.Item(i).Base_Uom, ZBABI_INFOR.Item(i).Delyg_Plnt, ZBABI_INFOR.Item(i).Matl_Desc, ZBABI_INFOR.Item(i).Text_Line, ZBABI_INFOR.Item(i).Taxclass, ZBABI_INFOR.Item(i).Division, conn, tran) = 0) Then
                        If (clsFc.InsertMeterial(ZBABI_INFOR.Item(i).Material, ZBABI_INFOR.Item(i).Base_Uom, ZBABI_INFOR.Item(i).Delyg_Plnt, ZBABI_INFOR.Item(i).Matl_Desc, ZBABI_INFOR.Item(i).Text_Line, ZBABI_INFOR.Item(i).Taxclass, "", "A", "A", ZBABI_INFOR.Item(i).Division, "A", "A", "A", "A", "05", conn, tran) < 1) Then
                            condition = False
                        End If
                    End If

                Next
                For i As Integer = 0 To ZBABI_PRICE.Count - 1
                    If (clsFc.UpdateSubMeterial(ZBABI_PRICE.Item(i).Material, ZBABI_PRICE.Item(i).Tabname, ZBABI_PRICE.Item(i).Vkorg, ZBABI_PRICE.Item(i).Vtweg, ZBABI_PRICE.Item(i).Werks, ZBABI_PRICE.Item(i).Kunnr, ZBABI_PRICE.Item(i).Datab, ZBABI_PRICE.Item(i).Datbi, ZBABI_PRICE.Item(i).Kbetr, ZBABI_PRICE.Item(i).Konwa, ZBABI_PRICE.Item(i).Kpein, ZBABI_PRICE.Item(i).Kmein, conn, tran) = 0) Then
                        If (clsFc.InsertSubMeterial(ZBABI_PRICE.Item(i).Material, ZBABI_PRICE.Item(i).Tabname, ZBABI_PRICE.Item(i).Vkorg, ZBABI_PRICE.Item(i).Vtweg, ZBABI_PRICE.Item(i).Werks, ZBABI_PRICE.Item(i).Kunnr, ZBABI_PRICE.Item(i).Datab, ZBABI_PRICE.Item(i).Datbi, ZBABI_PRICE.Item(i).Kbetr, ZBABI_PRICE.Item(i).Konwa, ZBABI_PRICE.Item(i).Kpein, ZBABI_PRICE.Item(i).Kmein, conn, tran) < 1) Then
                            condition = False
                        End If
                    End If
                Next
                clsFc.UpdateSubMeterialUnit(conn, tran)
                If (condition) Then
                    clsDB.CommitTran(tran)
                    ' Response.Write("<SCRIPT>alert('บันทึกข้อมูลสำเร็จ');</SCRIPT>")
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ !!');", True)
                Else
                    clsDB.RollBackTran(tran)
                    ' Response.Write("<SCRIPT>alert('เกิดความผิดพลาดในการบันทึก');</SCRIPT>")
                    ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึกข้อมูล !!');", True)
                End If
                conn.Close()
                Application.UnLock()
                ZBABI_INFOR.Clear()
                ZBABI_PRICE.Clear()
                conn.Dispose()
            End If
        Catch ex As Exception

        End Try
        ClassSAP.SAP_CLOSE(SAP_Data)
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        ViewState("Tariff") = clsGet.Get_MeterailMaster(txtMaterial.Text)
        gridTariff.DataSource = ViewState("Tariff")
        gridTariff.DataBind()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class